#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <readline/readline.h>
#include <readline/history.h>

char fis[20][20];

void cmd_chown(char *comm)
{
	char *c;
	uid_t owner;
	gid_t gr;
	int i=1,j,ok_v,ok_R,ch,rez;
	c = strtok (comm, " ");
	if (strstr (comm, ":") != NULL)
	{
		owner = (uid_t)strtok (NULL, ":");
		gr = (gid_t)strtok (NULL, " ");
	}
	else
	{
		owner = (uid_t)strtok (NULL, " ");
		gr = getgid();
	}
	while ((c = strtok (NULL, " ")) != NULL)
	{
		if (strcmp (c, "--help") == 0)
		{
			FILE *h = fopen ("help_chmod.txt", "r");
			while ((ch = fgetc (h)) != EOF)
				fputc (ch, stdout);
			fclose (h);
			return;
		}
		if (strcmp (c, "-v") == 0)
			ok_v = 1;
		else if (strcmp (c, "-R") == 0)
			ok_R = 1;
		else if ((strcmp (c, "-Rv") == 0) || (strcmp (c, "-vR") == 0))
		{
			ok_R = 1;
			ok_v = 1;
		}
		else 
		{
			strcpy (fis[i], c);
			i++;
		}
	}
	i--;
	if (i)
		for (j = 1; j <= i; j++)
		{
			rez = chown(fis[i],owner,gr); 
			fprintf (stderr, "rez = %d\n", rez);
		}
	else
		fprintf (stderr, "error!\n");
}

int main()
{
	char *comm;
	comm = readline ("command: ");
	cmd_chown(comm);
	return 0;
}
